var User = require('../../models/user/user')
var mailUtil = require('../../utility/mailUtil')

module.exports = {
    register: function(req, res) {

        var body = req.body

        User.findOne({name: body.name}, function(err, user) {
            if (err) {
                console.log('userController register fail. err = ' + err)
                return res.json({code: 177, msg: '操作失败'})
            }
            if (user) {
                return res.json({code: 122,  msg: '用户名已存在'})
            }
            new User({name: body.name, password: body.password}).save(function (err, user) {
                if (err) {
                    console.log('userController register fail. err = ' + err)
                    return res.json({code: 177, msg: '注册失败'})
                }
                return res.json({code: 100, data: {user: user}})
            })
        })
    },
    login: function(req, res) {
        var query = req.query
        User.findOne({name: query.name}, function (err, user) {
            if (err) {
                console.log('userController login fail. err = ' + err)
                return res.json({code: 177, msg: '登陆失败'})
            }
            if (!user) {
                return res.json({code: 122, msg: '用户未注册'})
            }
            if (user.password == query.password) {
                return res.json({code: 100, data: {user: user}})
            } else {
                return res.json({code: 122, msg: '密码错误'})
            }
        })
    },
    getList: function (req, res) {
        var skip = + req.params.skip
        var limit = + req.params.limit

        User.find({}).sort({createdAt: -1}).skip(skip).limit(limit).exec(function (err, users) {
            if (err) {
                console.log('userController getList fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            if (!users.length) {
                return res.json({code: 112, msg: "未找到数据"})
            }
            return res.json({code: 100, data: {list: users}})
        })

    },
    getUserInfo: function(req, res) {
        var id = req.params.id

        User.findById(id, function(err, user) {
            if (err) {
                console.log('userController getUserInfo fail. err = ' + err)
                return res.json({code: 177, msg: err})
            }
            if (!user) {
                return res.json({code: 112, msg: '没有找到该用户'})
            }
            return res.json({code: 100, msg: '查找成功', data: {user: user}})
        })
    },
    updateUserInfo: function(req, res) {
        var id = req.params.id

        var query = req.query

        //  如果有验证码，检查验证码是否错误或者接收验证码的邮箱是否错误
        if (query.verifyCode) {
            if (query.verifyCode != req.session.verifyCode) {
                return res.json({code: 122, msg: '验证码错误'})
            }
            if (req.session.email != query.email) {
                return res.json({code: 122, msg: '邮箱已更换，请重新获取验证码'})
            }
        }
        //  以上验证通过，如果有密码就验证密码再更新，如果没有直接更新
        if (query.password) {
            User.findById(id, function (err, user) {
                if (err) {
                    console.log('userController updateUserInfo fail. err = ' + err)
                    return res.json({code: 177, msg: '更新失败'})
                }
                if (user.password == query.password) {
                    updateUser(id, req.body, res)
                } else {
                    return res.json({code: 122, msg: '密码错误'})
                }
            })
        } else {
            updateUser(id, req.body, res)
        }

    },
    remove: function (req, res) {
        var id = req.params.id

        User.remove({_id: id}, function (err) {
            if (err) {
                console.log('userController remove fail. err = ' + err)
                return res.json({code: 177, msg: '删除失败'})
            }
            return res.json({code: 100, msg: '删除成功'})
        })
        
    },
    //  获取验证码
    getVerifyCode: function(req, res) {
        var email = req.query.email
        var code = Math.random().toString().slice(-4)
        req.session.verifyCode = code
        req.session.email = email
        mailUtil.getVerifyCode(email, code, function (err) {
            if (err) {
                console.log('userController getVerifyCode fail. err = ' + err)
                return res.json({code: 177, msg: '发送失败'})
            }
            return res.json({code: 100, msg: '发送成功'})
        })
        
    },
    changePassword: function(req, res) {

        var id = req.params.id
        var body = req.body
        var newPassword = body.newPassword

        User.findOneAndUpdate({_id: id}, {password: newPassword}, function (err, user) {
            if (err) {
                console.log('userController changePassword fail. err = ' + err)
                return res.json({code: 177, msg: '更改失败'})
            }
            return res.json({code: 100, msg: '修改成功', data: {user: user}})
        })
    }
    
}

function updateUser(id, update, res) {

    User.findOneAndUpdate({_id: id}, update, {upsert: true, new: true}, function (err, user) {
        if (err) {
            console.log('userController updateUserInfo fail. err = ' + err)
            return res.json({code: 177, msg: err})
        }
        return res.json({ code: 100, msg: '更新成功', data: {user: user}})
    })
}